﻿using Microsoft.AspNetCore.Mvc;
using Quartz.Api.Attr;
using Quartz.Api.Model;
using Quartz.Api.Severs;
using Quartz.Spi;
using System.Collections.Generic;
using System.Threading.Tasks;
using static Quartz.Api.GlobalVar.CustomApiVersion;

namespace Quartz.Api.Controllers
{
    [ApiController]
    [CustomRoute(ApiVersions.V1)]
    public class TaskscheController : Controller
    {
        private readonly ISchedulerFactory _schedulerFactory;
        private readonly IJobFactory _jobFactory;

        public TaskscheController(ISchedulerFactory schedulerFactory, IJobFactory jobFactory)
        {
            _schedulerFactory = schedulerFactory;
            _jobFactory = jobFactory;
        }

        /// <summary>
        ///  获取所有作业
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetJobs(int pagenum = 1,int pagesize = 2)
        {
            return Json( await _schedulerFactory.GetJobs(pagenum, pagesize));
        }

        /// <summary>
        /// 获取作业的运行日志
        /// </summary>
        /// <param name="taskName"></param>
        /// <param name="groupName"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRunLog(string taskName, string groupName, int page = 1)
        {
            return Json(QuartzExtension.GetJobRunLog(taskName, groupName, page));
        }
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add(TaskOptions taskOptions)
        {
            return Json(await taskOptions.AddJob(_schedulerFactory, jobFactory: _jobFactory));
        }

        /// <summary>
        /// 移除任务
        /// </summary>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Remove(TaskOptions taskOptions)
        {
            return Json(await _schedulerFactory.Remove(taskOptions));
        }

        /// <summary>
        /// 更新任务
        /// </summary>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update(TaskOptions taskOptions)
        {
            return Json(await _schedulerFactory.Update(taskOptions));
        }

        /// <summary>
        /// 暂停任务
        /// </summary>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Pause(TaskOptions taskOptions)
        {
            return Json(await _schedulerFactory.Pause(taskOptions));
        }
        /// <summary>
        /// 开始任务
        /// </summary>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Start(TaskOptions taskOptions)
        {
            return Json(await _schedulerFactory.Start(taskOptions));
        }

        /// <summary>
        /// 立即执行一次任务 
        /// </summary>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Run(TaskOptions taskOptions)
        {
            return Json(await _schedulerFactory.Run(taskOptions));
        }
    }
}